﻿/// <reference path="SampleDataGenComponent/SampleDataGenComponent.js"/>
/// <reference path="MicrosoftAjaxLibrary/System.Web.Extensions/1.0.61025.0/OpenAjaxExtensions.js"/>

// Application initialization code
OpenAjax.hub.subscribe(
    "org.openajax.interopfest10.bodyload", 
    function() {
        // Prepare the stock list
        var corpList = new SampleDataGenComponent.CorpList();
        corpList.addCorp("STRA", "Strawberry Computers", 100);
        corpList.addCorp("TITA", "Titan Power", 100);
        corpList.addCorp("EBEL", "East Bay Electric", 100);
        corpList.addCorp("MOOP", "Venetian Executive Search", 100);
        corpList.addCorp("HLSY", "Hall Systems", 100);
        corpList.addCorp("ULSO", "Ultisoft Systems", 100);
        corpList.addCorp("LATR", "Lattern Circuits", 100);
        corpList.addCorp("YSAM", "West Pecos Inc", 100);
        var corpArray = [];
        var corpAssocArray = {};
        
        // Map the Microsoft Ajax quote component's event to the OpenAjax hub
        Sys.OpenAjax.Helper.mapEventToMessage(
            corpList, "quoteChanged",
            "org.openajax.interopfest10.datagen.stockpriceupdate",
            function (sender, args) {
                // map the Microsoft Ajax component's event args to the OpenAjax
                // message payload:
                var quote = args.get_quote();
                return {
                    tickerName: quote.symbol,
                    corpName: quote.name,
                    price: quote.price
                };
            }
        );

        // subscribe to the OpenAjax update message and update the DOM
        OpenAjax.hub.subscribe(
            "org.openajax.interopfest10.datagen.stockpriceupdate", 
            function(eventName, stock) {
	            // Event "org.openajax.interopfest10.datagen.stockpriceupdate" passes a payload
	            // (called "stock" here) which is an object with these properties: 
	            //     {tickerName:<string>, corpName:<string>, price:<number>}
	            var delta = null;
	            if (corpAssocArray[stock.tickerName] == null) {
		            // This is the first time we have seen this particular stock.
		            // So, add it to corpAssocArray, append it to corpArray, re-sort corpArray.
		            corpAssocArray[stock.tickerName] = stock;
		            corpArray.push(stock.tickerName);
		            corpArray.sort();
	            } else {
		            delta = stock.price - corpAssocArray[stock.tickerName].price;
		            corpAssocArray[stock.tickerName].price = stock.price;
	            }
	            var eighths = ["", " 1/8", " 1/4", " 3/8", " 1/2", " 5/8", " 3/4", " 7/8"];
	            var s = ['\n<table border="1">\n<tbody>\n'];
	            for (var i = 0; i < corpArray.length; i++) {
	                var current = corpAssocArray[corpArray[i]];
		            var tickerName = current.tickerName;
		            var price = current.price;
		            var whole = Math.floor(price);
		            var eighth = Math.floor((price - whole) * 8);
		            if ((tickerName === stock.tickerName) && delta != null) {
			            if (delta < 0) {
				            s.push('<tr style="background-color:red">');
			            } else if (delta > 0) {
				            s.push('<tr style="background-color:green">');
			            } else {
				            s.push('<tr>');
			            }
		            } else {
			            s.push('<tr>');
		            }
		            s.push('<td>' + current.corpName +
		                '</td><td>' + tickerName +
		                '</td><td>' + whole + eighths[eighth] +
		                '</td></tr>\n'); 
	            }
	            s.push('</tbody>\n</table>\n');
	            document.getElementById("DataVis_1").innerHTML = s.join('');
            }
        );
        
        // Publish the refresh event for the results component
        var timeoutCookie = null;
        var timeoutCB = function() {
            OpenAjax.hub.publish("org.openajax.interopfest10.refreshResults", null);
            timeoutCookie = setTimeout(timeoutCB, 250);
        }
        timeoutCB();
        
        // Hook up the pause button
        Sys.UI.DomEvent.addHandler(
            $get("pause_resume"), "click",
            function(e) {
                corpList.toggleRunningState();
                if (corpList.get_running()) {
                    e.target.value = "Pause";
                    timeoutCB();
                }
                else {
                    e.target.value = "Resume";
                    clearTimeout(timeoutCookie);
                }
            }
        );
        
        // Start the list
        corpList.set_running(true);
    }
);
